package top.lixunda.ecommerce.server.good.api.entity.dto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ImmutableMap;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.api.condition.ILambdaQueryWrapperDTO;
import top.lixunda.ecommerce.server.business.api.entity.dto.OrderByDTO;
import top.lixunda.ecommerce.server.good.api.entity.db.FileCollectionDO;

import java.util.List;
import java.util.Map;

/**
 * FileCollection查询DTO
 * 用于接收并创造FileCollection数据的查询条件
 *
 * @author lixunda
 * @version time:2020/3/25 9:19
 */
@ApiModel(value = "FileCollection查询DTO",
        description = "用于接收并创造FileCollection数据的查询条件")
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
public class FileCollectionQueryDTO extends OrderByDTO
        implements ILambdaQueryWrapperDTO<FileCollectionDO> {

    @ApiModelProperty("名称，用于标志一类文件")
    private List<String> name;

    @ApiModelProperty("文件类型")
    private List<String> fileType;

    @ApiModelProperty("创建人员类型，USER STORE ADMIN")
    private List<String> createUserType;

    @ApiModelProperty("版本号")
    private Integer version;

    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private static final Map<String, SFunction<FileCollectionDO, ?>> MAP =
            new ImmutableMap.Builder<String, SFunction<FileCollectionDO, ?>>()
                    .put("name", FileCollectionDO::getName)
                    .put("url", FileCollectionDO::getUrl)
                    .put("fileId", FileCollectionDO::getFileId)
                    .put("fileType", FileCollectionDO::getFileType)
                    .put("version", FileCollectionDO::getVersion)
                    .put("createTime", FileCollectionDO::getCreateTime)
                    .build();

    /**
     * 将当前用于查询的DTO中的查询数据转换为 LambdaQuerySqlWrapper
     *
     * @return 条件构造器 LambdaQuerySqlWrapper
     */
    @Override
    public LambdaQueryWrapper<FileCollectionDO> toWrapper() {
        LambdaQueryWrapper<FileCollectionDO> wrapper = new LambdaQueryWrapper<>();
        if (ObjectValidators.isNotEmpty(name)) {
            wrapper.and(innerWrapper ->
                    this.name.forEach(item ->
                            innerWrapper.or().eq(FileCollectionDO::getName, item)));
        }
        if (ObjectValidators.isNotEmpty(fileType)) {
            wrapper.and(innerWrapper ->
                    this.fileType.forEach(item ->
                            innerWrapper.or().eq(FileCollectionDO::getFileType, item)));
        }
        if (ObjectValidators.isNotEmpty(createUserType)) {
            wrapper.and(innerWrapper ->
                    this.createUserType.forEach(item ->
                            innerWrapper.or().eq(FileCollectionDO::getCreateUserType, item)));
        }
        if (ObjectValidators.isNotEmpty(version)) {
            wrapper.eq(FileCollectionDO::getVersion, version);
        }
        dealWithOrderBy(wrapper, MAP);
        return wrapper;
    }
}
